package ru.ifmo.mlarinsky.classificator.xml;

import java.util.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ru.ifmo.mlarinsky.classificator.model.Vector;
import ru.ifmo.mlarinsky.classificator.providers.VectorProvider;

/**
* @author Людочка
*/
public final class VectorsXMLBuilder extends XMLBuilder {
    protected Element buildRootElement(Document document, Object dataProvider)
            throws IllegalArgumentException {
        if (!(dataProvider instanceof VectorProvider)) {
            throw new IllegalArgumentException("dataProvider should be list of VectorProvider");
        }

        VectorProvider vectorProvider = (VectorProvider) dataProvider;

        String[] dimensions = vectorProvider.getDimensions();
        List<Vector> vectors = vectorProvider.getVectors();

        Element root = document.createElement("jsom");
        Element pref = document.createElement("pref");

//        pref.appendChild(buildDimensionElement(document, dimensions));

        root.appendChild(pref);
//        root.appendChild(buildDataElement(document, vectors));

        return root;
    }
//
    @Override
    protected String buildHeader() {
        return "";
    }
//
//    private static Element buildDimensionElement(Document doc, String[] dimensions) {
//        Element dimensionElement = doc.createElement("dimension");
//
//        for (String dimensionName : dimensions) {
//            Element dimTypeElement = doc.createElement("dim_type");
//            Text dimTypeName = doc.createTextNode(dimensionName);
//
//            dimTypeElement.appendChild(dimTypeName);
//            dimensionElement.appendChild(dimTypeElement);
//        }
//
//        return dimensionElement;
//    }
//
//    private static Element buildDataElement(Document doc, List<Vector> vectors) {
//        Element dataElement = doc.createElement("data");
//
//        for (Vector vector : vectors) {
//            dataElement.appendChild(buildNodeElement(doc, vector));
//        }
//
//        return dataElement;
//    }
//
//    private static Element buildNodeElement(Document doc, Vector vector) {
//        Element node = doc.createElement("node");
//        node.setAttribute("label", vector.getLabel());
//
//        for (Map.Entry<String, Double> coordinate : vector.getCoordinates().entrySet()) {
//            node.appendChild(buildValueElement(doc,
//                    coordinate.getKey(), String.valueOf(coordinate.getValue())));
//        }
//
//        return node;
//    }
//
//    private static Element buildValueElement(Document doc, String dimensionName, String value) {
//        Element valueElement = doc.createElement("dim");
//        valueElement.setAttribute("type", dimensionName);
//
//        Text valueText = doc.createTextNode(value);
//        valueElement.appendChild(valueText);
//
//        return valueElement;
//    }
//
//    ///////////////////////////// Testing /////////////////////////////////
//    public static void main(String[] args) {
//        try {
////            testBuildDimensions();
////            testBuildXmlString();
//            testWrite();
//        } catch (Throwable e) {
//            System.out.println("\nSystem error. Caused by:");
//            e.printStackTrace(System.out);
//        }
//    }
//
//    private static void testBuildDimensions() {
//        VectorProvider vectorProvider = new DeterminationVectorProvider();
//
//        String[] dimensions = vectorProvider.getDimensions();
//
//        System.out.println("Dimensions:");
//        for (String dimension : dimensions) {
//            System.out.println(dimension);
//        }
//    }
//
//    private static void testBuildXmlString() {
////        VectorsXMLBuilder xmlBuilder = new VectorsXMLBuilder();
////
////        String xmlString = xmlBuilder.buildXMLString(new DeterminationVectorProvider());
////
////        System.out.println("xmlString = " + xmlString);
//    }
//
//    private static void testWrite() {
////        VectorsXMLBuilder xmlBuilder = new VectorsXMLBuilder();
////
////        String xmlString = xmlBuilder.buildXMLString(new DeterminationVectorProvider());
////
////        IOUtil.write(xmlString, "full_data_test.xml");
//    }
}
